package cn.atzxc.qy.service.impl;

import cn.atzxc.qy.domain.Address;
import cn.atzxc.qy.domain.LoginUser;
import cn.atzxc.qy.domain.User;
import cn.atzxc.qy.mapper.UserMapper;
import cn.atzxc.qy.service.UserService;
import cn.atzxc.qy.utils.JwtUtil;
import cn.atzxc.qy.utils.RedisCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户信息服务
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserMapper userMapper;

    @Override
    public User redisGetUserInfo(String subject) {
        //从redis中获取用户信息
        LoginUser cacheObject = redisCache.getCacheObject("login:" + subject);
        User user = cacheObject.getUser();
        //如果获取到的用户为空说明用户没有登录，则提示登录
        if (user == null) {
            throw new RuntimeException("redis中无法查到信息");
        }
        return user;
    }

    /**
     * 根据用户ID，更新redis中的值
     * @param token
     * @return
     */
    @Override
    public User redisSetUserInfo(String token,User user) throws Exception {

        //通过token解析出指定的用户名ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();

        LoginUser cacheObject = redisCache.getCacheObject("login:" + subject);

        cacheObject.setUser(user);
        redisCache.setCacheObject("login:" + subject, cacheObject);
        return user;
    }

    /**
     * 根据ID更新用户所属学校
     * 1.根据token获取到指定用户的ID
     * 2.根据用户ID把指定字段的值，更新为选中的大学
     * @param token
     * @return
     */
    @Override
    public User updateCollegeById(String token, String collegeName) throws Exception {
        //1.通过token解析出指定的用户名ID
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        System.out.println(subject);
        //2.通过ID获取到指定的用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUser_id, subject);
        User user = userMapper.selectOne(queryWrapper);
        user.setUniversity(collegeName);
        //3.更新指定字段的指定值 (也可以说是重新插入)
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUser_id, subject); // 根据地址ID，设置修改条件
        int updateNum = userMapper.update(user, updateWrapper);
        System.out.println("查询到后，更改的值为："+ user);

        return user;
    }

    @Override
    public int updateUserInfo(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUser_id,user.getUser_id());
        updateWrapper.set(User::getAvatarImage,user.getAvatarImage());
        updateWrapper.set(User::getPhoneNumber,user.getPhoneNumber());
        updateWrapper.set(User::getUsername,user.getUsername());
        updateWrapper.set(User::getGender,user.getGender());
        int update = userMapper.update(null, updateWrapper);
        return update;
    }

//    @Override
//    public User getUserInfoAll(String user_id) {
//        User userWithSchoolAndCampus = userMapper.findUserWithSchoolAndCampus(user_id);
//        // 如果查询不到信息,说明用户没有登录
//        if (userWithSchoolAndCampus == null) {
//            throw new RuntimeException("没有相关信息");
//        }
//        System.out.println("查询出来的用户信息为："+userWithSchoolAndCampus);
//        return userWithSchoolAndCampus;
//    }

}
